package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil; 
import java.math.BigDecimal;

@Repository("FoodbatchfillerDAO")
@Transactional
public class FoodbatchfillerDAOImpl extends AbstractJpaDao<Foodbatchfiller> implements FoodbatchfillerDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Foodbatchfiller.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public FoodbatchfillerDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Foodbatchfiller findFoodbatchfillerById(Integer id) throws DataAccessException {

		return findFoodbatchfillerById(id, -1, -1);
	}

	@Transactional
	public Foodbatchfiller findFoodbatchfillerById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchfillerById", startResult, maxRows, id);
			return (Foodbatchfiller) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodbatchfiller> findFoodbatchfillerByName(String name) throws DataAccessException {

		return findFoodbatchfillerByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchfiller> findFoodbatchfillerByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchfillerByName", startResult, maxRows, name);
		return new LinkedHashSet<Foodbatchfiller>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatchfiller> findFoodbatchfillerByNameContaining(String name) throws DataAccessException {

		return findFoodbatchfillerByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchfiller> findFoodbatchfillerByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchfillerByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Foodbatchfiller>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatchfiller> findFoodbatchfillerByQuantity(BigDecimal quantity) throws DataAccessException {

		return findFoodbatchfillerByQuantity(quantity, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchfiller> findFoodbatchfillerByQuantity(BigDecimal quantity, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchfillerByQuantity", startResult, maxRows, quantity);
		return new LinkedHashSet<Foodbatchfiller>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatchfiller> findFoodbatchfillerByDescription(String description) throws DataAccessException {

		return findFoodbatchfillerByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchfiller> findFoodbatchfillerByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchfillerByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Foodbatchfiller>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatchfiller> findFoodbatchfillerByDescriptionContaining(String description)
			throws DataAccessException {

		return findFoodbatchfillerByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchfiller> findFoodbatchfillerByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchfillerByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Foodbatchfiller>(query.getResultList());
	}

	@Transactional
	public Foodbatchfiller findFoodbatchfillerByPrimaryKey(Integer id) throws DataAccessException {
		return findFoodbatchfillerByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Foodbatchfiller findFoodbatchfillerByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchfillerByPrimaryKey", startResult, maxRows, id);
			return (Foodbatchfiller) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodbatchfiller> findFoodbatchfillersByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchfillersByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodbatchfiller> findAllFoodbatchfillers() throws DataAccessException {

		return findAllFoodbatchfillers(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchfiller> findAllFoodbatchfillers(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodbatchfillers", startResult, maxRows);
		return new LinkedHashSet<Foodbatchfiller>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodbatchfiller> findFoodbatchfillersByFiller(Integer fillerId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFoodbatchfillersByFiller", start, max, fillerId);
		return new LinkedHashSet<Foodbatchfiller>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodbatchfiller> findFoodbatchfillersByFoodbatch(Integer foodbatchId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFoodbatchfillersByFoodbatch", start, max, foodbatchId);
		return new LinkedHashSet<Foodbatchfiller>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodbatchfiller> findFoodbatchfillersByUnit(Integer unitId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFoodbatchfillersByUnit", start, max, unitId);
		return new LinkedHashSet<Foodbatchfiller>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchfiller> findFoodbatchfillerByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Foodbatchfiller>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Foodbatchfiller> findFoodbatchfillersByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Foodbatchfiller.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Foodbatchfiller> foodbatchfillers = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return foodbatchfillers;
	}

	@Transactional
	public Foodbatchfiller findFoodbatchfillerByPhysicalPrimaryKey(Integer foodbatchId, Integer fillerId) throws DataAccessException {
		return findFoodbatchfillerByPhysicalPrimaryKey(foodbatchId, fillerId, -1, -1);
	}

	@Transactional
	public Foodbatchfiller findFoodbatchfillerByPhysicalPrimaryKey(Integer foodbatchId, Integer fillerId,
			int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchfillerByPhyscialPrimaryKey", startResult, maxRows, foodbatchId,
					fillerId);
			return (Foodbatchfiller) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Foodbatchfiller entity) {
		return true;
	}


}
